package com.example.pwj.ffstudy.ui.zhihu.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.example.pwj.ffstudy.R;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyComment;
import com.example.pwj.ffstudy.bean.zdaily.ZDailyCommentSection;
import com.example.pwj.ffstudy.comm.CommSectionAdapter;
import com.example.pwj.ffstudy.comm.CommViewHolder;
import com.example.pwj.ffstudy.ui.zhihu.ZhiConst;
import com.example.pwj.ffstudy.util.EventBusUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pwj on 2017/8/28.
 * 只有长评论和短评论2个头
 */

public class ZNewsCommAdapter extends CommSectionAdapter<ZDailyCommentSection> {
    private List<ZDailyCommentSection> sections = new ArrayList<>();
    private Map<String, Boolean> map = new HashMap<>();
    private int temp = 0;
    public ZNewsCommAdapter(Context context) {
        super(context, R.layout.item_znews_comment, R.layout.item_comment_header);
    }

    @Override
    protected void convertSection(final CommViewHolder holder, final ZDailyCommentSection zDailyCommentSection) {
        holder.setText(R.id.tv_zhihu_section, zDailyCommentSection.header);
        holder.setVisible(R.id.iv_direct, !zDailyCommentSection.isLong);
        holder.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                temp++;
                if (!zDailyCommentSection.isLong) {
                    if (temp % 2 == 0) {
                        holder.setImageResource(R.id.iv_direct, R.mipmap.ic_down);
                        map.put(ZhiConst.COMMENT_IS_EXPAND, false);
                        close();
                    } else {
                        //展开
                        holder.setImageResource(R.id.iv_direct, R.mipmap.ic_up);
                        map.put(ZhiConst.COMMENT_IS_EXPAND, true);
                    }
                    EventBusUtils.post(map);
                }
            }
        });
    }

    /**
     * 展开
     * @param sections
     */
    public void expand(List<ZDailyCommentSection> sections) {
        this.sections = sections;
        mData.addAll(sections);
        notifyDataSetChanged();
    }

    /**
     * 分页查询时的数据变化
     * @param sections
     */
    public void addExpand(List<ZDailyCommentSection> sections) {
        this.sections.addAll(sections);
        mData.addAll(sections);
        notifyDataSetChanged();
    }

    /**
     * 关闭
     */
    public void close() {
        mData.removeAll(this.sections);
        LogUtils.e("sections:"+sections.size());
        LogUtils.e("mData:"+mData.size());
        this.sections.clear();
        notifyDataSetChanged();
    }
    @Override
    public void convert(CommViewHolder holder, ZDailyCommentSection section) {
        ZDailyComment zDailyComment = section.t;
        holder.setImageUrl(R.id.iv_avatar, zDailyComment.getAvatar())
                .setText(R.id.tv_author, zDailyComment.getAuthor())
                .setText(R.id.tv_likes, String.valueOf(zDailyComment.getLikes()));
        @SuppressLint("SimpleDateFormat")
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm");
        String date = TimeUtils.millis2String(zDailyComment.getTime(), dateFormat);
        holder.setText(R.id.tv_time, date);
        ZDailyComment.ReplyToBean reply_to = zDailyComment.getReply_to();
        String author = "";
        String reply_content = "";
        String content = "";
        if (reply_to != null) {
            author = reply_to.getAuthor();
            reply_content = reply_to.getContent();
            content = zDailyComment.getContent() + "\n" + "//" + author + ":" + reply_content;
        } else {
            content = zDailyComment.getContent();
        }
        holder.setText(R.id.tv_content, content);
    }
}
